package com.sprouting.personal.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.personal.annotations.ApiIdempotent;
import com.sprouting.personal.model.common.ResultJson;
import com.sprouting.personal.model.common.jsr.AddGroup;
import com.sprouting.personal.model.common.jsr.UpdGroup;
import com.sprouting.personal.model.entity.TaskSign;
import com.sprouting.personal.model.entity.TaskSignDetail;
import com.sprouting.personal.service.TaskSignDetailService;
import com.sprouting.personal.service.TaskSignService;
import com.sprouting.personal.utils.CheckTool;
import com.sprouting.personal.utils.EntityTool;
import com.sprouting.personal.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 签到组 控制层
 *
 * @author ：sprouting
 * @date 创建时间： 2023-07-02 12:27:51
 */
@Slf4j
@RestController
@RequestMapping("taskSign")
public class TaskSignController {

	@Autowired
	private TaskSignService taskSignService;
	@Autowired
	private TaskSignDetailService taskSignDetailService;


	/**
	 * 保存签到主表信息
	 * @param taskSign 签到主表
	 * @return
	 */
	@ApiIdempotent
	@PostMapping("/saveTaskSign")
	public ResultJson saveTaskSign(@Validated(AddGroup.class) TaskSign taskSign){
		taskSign.setId(IdTool.getId());
		taskSign.setUserId(StpUtil.getLoginIdAsString());
		EntityTool.buildSaveBase(taskSign, StpUtil.getLoginIdAsString());
		taskSignService.save(taskSign);
		return ResultJson.success(taskSign);
	}

	/**
	 * 更新签到主表
	 * @param taskSign 更新一个名录
	 * @return
	 */
	@ApiIdempotent
	@PostMapping("/updTaskSign")
	public ResultJson updTaskSign(@Validated(UpdGroup.class) TaskSign taskSign){
		EntityTool.buildUpdBase(taskSign, StpUtil.getLoginIdAsString());
		taskSignService.updateById(taskSign);
		return ResultJson.success();
	}

	/**
	 * 删除签到主表
	 * @param id 签到主表主键
	 * @return
	 */
	@ApiIdempotent
	@PostMapping("/delTaskSignById")
	public ResultJson delTaskSignById(String id){
		if (CheckTool.checkNull(id)){
			return ResultJson.errorIllegal();
		}
		taskSignService.delTaskSignById(id);
		return ResultJson.success();
	}

	/**
	 * 分页查询签到主表
	 * @param page 分页参数
	 * @param taskSign 查询参数
	 * @return
	 */
	@PostMapping("/getTaskSignListPage")
	public ResultJson getTaskSignListPage(Page page, TaskSign taskSign){
		if (taskSign == null){
			taskSign = new TaskSign();
		}
		taskSign.setUserId(StpUtil.getLoginIdAsString());
		return ResultJson.success(taskSignService.getTaskSignPage(page, taskSign));
	}


	/*
        签到数据明细表***************************
        签到数据明细表***************************
        签到数据明细表***************************
    * */

	/**
	 * 保存签到详情信息
	 * @param taskSignDetail 签到详情
	 * @return
	 */
	@PostMapping("/saveTaskSignDetail")
	@ApiIdempotent
	public ResultJson saveTaskSignDetail(@Validated(AddGroup.class) TaskSignDetail taskSignDetail){
		TaskSignDetail db = taskSignDetailService.getBySignTime(taskSignDetail.getTaskSignId(), taskSignDetail.getSignTime());
		if (db != null){
			return ResultJson.success();
		}

		taskSignDetail.setId(IdTool.getId());
		taskSignDetail.setUserId(StpUtil.getLoginIdAsString());
		EntityTool.buildSaveBase(taskSignDetail, StpUtil.getLoginIdAsString());
		taskSignDetailService.save(taskSignDetail);
		return ResultJson.success(taskSignDetail);
	}

	/**
	 * 更新签到详情信息
	 * @param taskSignDetail 签到详情
	 * @return
	 */
	@ApiIdempotent
	@PostMapping("/updTaskSignDetail")
	public ResultJson updTaskSignDetail(@Validated(UpdGroup.class) TaskSignDetail taskSignDetail){
		EntityTool.buildUpdBase(taskSignDetail, StpUtil.getLoginIdAsString());
		taskSignDetailService.updateById(taskSignDetail);
		return ResultJson.success();
	}

	/**
	 * 根据主键删除签到详情信息
	 * @param time 要取消的时间
	 * @param taskSignId 签到任务主键
	 * @return
	 */
	@ApiIdempotent
	@PostMapping("/delTaskSignDetail")
	public ResultJson delTaskSignDetail(String signTime, String taskSignId){
		if (CheckTool.checkNull(signTime, taskSignId)){
			return ResultJson.errorIllegal();
		}
		DateTime parse = DateUtil.parse(signTime, "yyyy-MM-dd");
		TaskSignDetail db = taskSignDetailService.getBySignTime(taskSignId, parse);
		if (db == null){
			return ResultJson.errorIllegal();
		}

		taskSignDetailService.removeById(db);
		return ResultJson.success();
	}

	/**
	 * 查询 上月 + 本月 + 下个月的月经情况
	 * @param time 要查询的时间，不传则从当前时间开始
	 * @param taskSignId 签到任务主表id
	 * @return
	 */
	@PostMapping("/getMenstruation")
	public ResultJson getMenstruation(String time, String taskSignId){
		if (CheckTool.checkNull(taskSignId)){
			return ResultJson.errorIllegal();
		}
		Date date;
		if (CheckTool.checkNull(time)){
			date = new Date();
		} else {
			date = DateUtil.parseDate(time);
		}

		// 获取上个月初始日期和下个月结束日期
		DateTime before = DateUtil.offsetMonth(date, -1);
		DateTime after = DateUtil.offsetMonth(date, 1);
		DateTime beforeStart = DateUtil.beginOfMonth(before);
		DateTime afterEnd = DateUtil.endOfMonth(after);
		return ResultJson.success(taskSignDetailService.getMenstruationSectionTime(StpUtil.getLoginIdAsString(), taskSignId, beforeStart, afterEnd));
	}

}
